package inglar.autotc.client.ui;

import inglar.autotc.client.Utilities;
import inglar.autotc.shared.db.Actions;
import inglar.autotc.shared.db.Templates;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class DialogBoxAddAction extends DialogBox {
	private VerticalPanel contentPanel;
	private FlexTable tableDetect;
	private TextBox txtbxPrio;
	private ListBox listbxTemplates;

	public DialogBoxAddAction() {
		setHTML("Add Action");
		
		contentPanel = new VerticalPanel();
		contentPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		setWidget(contentPanel);
		
		FlexTable flexTable = new FlexTable();
		contentPanel.add(flexTable);
		
		Label lblPriority = new Label("Priority:");
		lblPriority.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(0, 0, lblPriority);
		
		txtbxPrio = new TextBox();
		txtbxPrio.setText("prio");
		flexTable.setWidget(0, 1, txtbxPrio);
		
		Label lblSetTemplate = new Label("Set template:");
		lblSetTemplate.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(1, 0, lblSetTemplate);
		
		listbxTemplates = new ListBox();
		flexTable.setWidget(1, 1, listbxTemplates);
		listbxTemplates.addItem("Loading . . .");
		
		AsyncCallback<Templates[]> callback = new AsyncCallback<Templates[]>() {
			@Override
			public void onSuccess(Templates[] result) {
				listbxTemplates.clear();
				if(result.length == 0) {
					Window.alert("Save a template first!");
					hide();
					return;
				}
				for (int i = 0; i < result.length; i++) {
					String value = null;
					String item = result[i].getName() + " (" + (value = result[i].getDevice()) + ")";
					listbxTemplates.addItem(item, value);
				}
			}
			@Override
			public void onFailure(Throwable caught) {
				Utilities.failureAlert("DialogBoxAddAction.getTemplateListCallback", "RPC failure", caught);
				hide();
			}
		};
		Utilities.getTemplateList(callback);
		
		Label lblDetect = new Label("Detect:");
		lblDetect.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(2, 0, lblDetect);
		
		Button btnAddRow = new Button("Add row");
		flexTable.setWidget(2, 1, btnAddRow);
		btnAddRow.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				addRow();
			}
		});
		
		tableDetect = new FlexTable();
		contentPanel.add(tableDetect);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		contentPanel.add(horizontalPanel);
		
		Button btnApply = new Button("Apply");
		horizontalPanel.add(btnApply);
		btnApply.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnApply.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Actions action = new Actions();
				try {
					action.setPrio(txtbxPrio.getValue());
				} catch (IllegalArgumentException iae) {
					Window.alert(iae.getMessage());
					return;
				} catch(Throwable caught) {
					Utilities.failureAlert("DialogBoxAddAction.btnApply.onClick", "Object field setting failure", caught);
					return;
				}
				int selected = listbxTemplates.getSelectedIndex();
				action.setAction("set " + listbxTemplates.getItemText(selected));
				int rows = tableDetect.getRowCount();
				for(int i = 0; i < rows; i++) {
					ListBox lb = (ListBox) tableDetect.getWidget(i, 0);
					selected = lb.getSelectedIndex();
					String dopts = lb.getItemText(selected);
					TextBox tb = (TextBox) tableDetect.getWidget(i, 1);
					String dval = tb.getText().trim();
					try {
						action.addDetect(dopts, dval);
					} catch (IllegalArgumentException iae) {
						//Utilities.failureAlert("DialogBoxAddAction.btnApply.onClick", "Object field setting failure", iae);
					}
				}
				
				AsyncCallback<Void> applyActionCallback = new AsyncCallback<Void>() {
					@Override
					public void onSuccess(Void result) {
						Window.alert("Successfully applied action.");
						Location.reload();
					}
					@Override
					public void onFailure(Throwable caught) {
						Utilities.failureAlert("DialogBoxAddAction.btnApply", "RPC failure", caught);
						hide();
					}
				};
				Utilities.setAction(action, applyActionCallback);
			}
		});
		
		Button btnCancel = new Button("Cancel");
		horizontalPanel.add(btnCancel);
		btnCancel.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnCancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		
		addRow(); // add an initial row
	}
	
	protected void addRow() {
		int row = tableDetect.getRowCount();
		
		String[] detectopts = Actions.detectOptions;
		
		ListBox detect = new ListBox();
		tableDetect.setWidget(row, 0, detect);
		for(int i = 0; i < detectopts.length; i++) detect.addItem(detectopts[i]);
		
		TextBox criteria = new TextBox();
		tableDetect.setWidget(row, 1, criteria);
		
		final Button delRow = new Button("<b>x</b>");
		tableDetect.setWidget(row, 2, delRow);
		delRow.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				int row = tableDetect.getRowCount();
				for(int i = 0; i < row; i++) {
					Widget w = tableDetect.getWidget(i, 2);
					if(w == delRow) {
						tableDetect.removeRow(i);
						break;
					}
				}
			}
		});
	}

}
